package service

import (
	sqlc "bgs/db/sqlc"
	orderUtil "bgs/service/order"
	"bgs/util"
	"context"
)

// CalcProfitSharedAmount 计算已分账金额
func (s *LedgerServiceImp) CalcProfitSharedAmount(ctx context.Context, q *sqlc.Queries, afterSalePtr *sqlc.AfterSale) (
	profitSharedAmount int64,
	err error) {

	var submchProfitAmount, platformCommission, distributorCommission, bonusAmount int64

	ledgerRequests, err := q.ListLedgerRequestsByOrderSerialNumberAndLedgerStatus(ctx, sqlc.ListLedgerRequestsByOrderSerialNumberAndLedgerStatusParams{
		OrderSerialNumber: afterSalePtr.OrderSerialNumber,
		LedgerStatuses:    []string{string(sqlc.EmLedgerStatusSubmitted), string(sqlc.EmLedgerStatusFinished)},
	})
	if err != nil {
		return
	}

	for _, rq := range ledgerRequests {
		submchProfitAmount += rq.SubmchProfitAmount
		platformCommission += rq.PlatformCommission
		distributorCommission += rq.DistributorCommission
		bonusAmount += rq.BonusAmount
	}

	ledgerRollbacks, err := q.ListLedgerRollbacksByOrderSerialNumberAndLedgerStatus(ctx, sqlc.ListLedgerRollbacksByOrderSerialNumberAndLedgerStatusParams{
		OrderSerialNumber: afterSalePtr.OrderSerialNumber,
		LedgerStatuses:    []string{string(sqlc.EmLedgerStatusSubmitted), string(sqlc.EmLedgerStatusFinished)},
	})
	if err != nil {
		return
	}

	for _, rb := range ledgerRollbacks {
		submchProfitAmount -= rb.SubmchProfitAmount
		platformCommission -= rb.PlatformCommission
		distributorCommission -= rb.DistributorCommission
		bonusAmount -= rb.BonusAmount
	}
	profitSharedAmount = submchProfitAmount + platformCommission + distributorCommission + bonusAmount
	return
}

// DoBeforeAfterSaleRefund 先分账回退再退款,目前如果子商户先分账,则只有在订单结束后有效
// func (s *LedgerServiceImp) DoBeforeAfterSaleRefund(ctx context.Context, q *sqlc.Queries, orderPtr *sqlc.Order, afterSalePtr *sqlc.AfterSale) (err error) {
// 	ledgerRequests, err := q.ListLedgerRequestsByOrderSerialNumberAndLedgerStatus(ctx, sqlc.ListLedgerRequestsByOrderSerialNumberAndLedgerStatusParams{
// 		OrderSerialNumber: afterSalePtr.OrderSerialNumber,
// 		LedgerStatuses:    []string{string(sqlc.EmLedgerStatusCreated), string(sqlc.EmLedgerStatusFinished)},
// 	})
// 	if err != nil {
// 		return err
// 	}
// 	if orderUtil.CheckIsOrderFinish(orderPtr) {
// 		// 订单已完结
// 		if afterSalePtr.IsWholeRefund {
// 			// VIP:售后全部退的已完成分账的创建分账回退,未提交的分账则关闭

// 			// 已分账的创建分账回退
// 			err = s.createLedgerRollbackAndDetailsForAfterSale(ctx, q, orderPtr, afterSalePtr, ledgerRequests)
// 			if err != nil {
// 				return
// 			}
// 			// 关闭剩余的非提交分账申请
// 			err = s.closeUnSubmitLedgerRequestsBySerialNumber(ctx, q, afterSalePtr.OrderSerialNumber)
// 			if err != nil {
// 				return err
// 			}
// 			s.slog.Infof("已完成订单售后单全部退款时分账处理完毕 ok")
// 		} else {
// 			// VIP:售后部分退的按比例更新未提交的分账,已完成分账的按比例创建分账回退

// 			// 更新未提交的分账申请
// 			err = s.updateUnSubmitLedgerRequestAndDetailsForAfterSale(ctx, q, orderPtr, afterSalePtr, ledgerRequests)
// 			if err != nil {
// 				return
// 			}
// 			s.slog.Infof("已完成订单售后单部分退款时分账处理完毕 ok")
// 			// 已分账的创建分账回退
// 			err = s.createLedgerRollbackAndDetailsForAfterSale(ctx, q, orderPtr, afterSalePtr, ledgerRequests)
// 			if err != nil {
// 				return
// 			}
// 			s.slog.Infof("已完成订单售后单部分退款时分账处理完毕 ok")
// 		}
// 	} else {
// 		s.slog.Infof("未完成订单在退款成功后处理...ok")
// 	}
// 	return
// }

// DoAfterAfterSaleRefundedSuccess 先退款后进行分账回退,目前如果子商户先分账,则只有在订单结束前有效
func (s *LedgerServiceImp) DoAfterAfterSaleRefundedSuccess(ctx context.Context, q *sqlc.Queries, orderPtr *sqlc.Order, afterSalePtr *sqlc.AfterSale) (isProfited bool, err error) {

	ledgerRequests, err := q.ListLedgerRequestsByOrderSerialNumberAndLedgerStatus(ctx, sqlc.ListLedgerRequestsByOrderSerialNumberAndLedgerStatusParams{
		OrderSerialNumber: afterSalePtr.OrderSerialNumber,
		LedgerStatuses:    []string{string(sqlc.EmLedgerStatusCreated), string(sqlc.EmLedgerStatusFinished)},
	})
	if err != nil {
		return
	}
	if orderUtil.CheckIsOrderFinish(orderPtr) {
		// 订单已完结
		if afterSalePtr.IsWholeRefund {
			// VIP:售后全部退的已完成分账的创建分账回退,未提交的分账则关闭
			// 目前订单完结还能进行售后一定是子商户自己的分账完成,但其他账户没有分账,因此子商户自己解冻的记录不用回退
			// 仅仅将其他未提交的分账请求关闭
			// 已分账的创建分账回退
			// err = s.createLedgerRollbackAndDetailsForAfterSale(ctx, q, orderPtr, afterSalePtr, ledgerRequests)
			// if err != nil {
			// 	return
			// }
			// 关闭剩余的非提交分账申请
			err = s.closeUnSubmitLedgerRequestsBySerialNumber(ctx, q, afterSalePtr.OrderSerialNumber)
			if err != nil {
				return
			}
			isProfited = true
			s.slog.Infof("已完成售后单全部退款时分账处理完毕 ok")
		} else {
			// VIP:售后部分退的按比例更新未提交的分账,已完成分账的按比例创建分账回退

			// 更新未提交的分账申请
			err = s.updateUnSubmitLedgerRequestAndDetailsForAfterSale(ctx, q, orderPtr, afterSalePtr, ledgerRequests)
			if err != nil {
				return
			}
			// 已分账的创建分账回退 (目前订单完结还能进行售后一定是子商户自己的分账完成,但其他账户没有分账,因此子商户自己解冻的记录不用回退)
			// err = s.createLedgerRollbackAndDetailsForAfterSale(ctx, q, orderPtr, afterSalePtr, ledgerRequests)
			// if err != nil {
			// 	return
			// }
			s.slog.Infof("已完成售后单部分退款时分账处理完毕 ok")
		}
	} else {
		// 订单未完成
		if afterSalePtr.IsWholeRefund {
			// VIP:售后全部退的未提交的分账全部关闭

			// 关闭非提交分账申请
			err = s.closeUnSubmitLedgerRequestsBySerialNumber(ctx, q, afterSalePtr.OrderSerialNumber)
			if err != nil {
				return
			}
			isProfited = true
			s.slog.Infof("未完成订单售后单全部退款时分账处理完毕 ok")
		} else {
			// VIP:售后部分退的按比例更新未提交的分账
			// 更新未提交的申请
			err = s.updateUnSubmitLedgerRequestAndDetailsForAfterSale(ctx, q, orderPtr, afterSalePtr, ledgerRequests)
			if err != nil {
				return
			}
			s.slog.Infof("未完成订单售后单部分退款时分账处理完毕 ok")
		}
	}
	return
}

// createLedgerRollbackAndDetailsForAfterSale 订单已完成且部分退场景->根据售后单创建分账回退和明细(分账回退和分账请求对应)
// func (s *LedgerServiceImp) createLedgerRollbackAndDetailsForAfterSale(ctx context.Context, q *sqlc.Queries, orderPtr *sqlc.Order, afterSalePtr *sqlc.AfterSale, ledgerRequests []sqlc.LedgerRequest) (err error) {
// 	slog := util.NewContextLogger(ctx, s.slog)
// 	afterSaleSerialNumber := afterSalePtr.SerialNumber

// 	// 对已完成的分账进行分账回退申请,只要对着之前订单完成的申请原样回退即可
// 	// 目前仅订单完成时触发的商户的冻结金额预先分账一条

// 	ledgerRequestPtrAtOrderStatusToFinish, ok := s.FindLedgerRequest(ledgerRequests, func(lr *sqlc.LedgerRequest) bool {
// 		return lr.LedgerStatus == string(sqlc.EmLedgerStatusFinished) && lr.ProfitTrigger == string(sqlc.EmProfitTriggerAtOrderStatusToFinish)
// 	})

// 	if ok {
// 		// 取已子商户分账的金额和退款金额中的小的那个作为回退金额
// 		submchProfitAmount := util.MinTwoInt64(ledgerRequestPtrAtOrderStatusToFinish.SubmchProfitAmount, afterSalePtr.Amount)
// 		slog.Infof("创建回退记录 submchProfitAmount=>%d", submchProfitAmount)
// 		ledgerRollback, ledgerRollDetailPtrs, err := s.CreateRollback(ctx, q, CreateRollbackParam{
// 			BrandID:               afterSalePtr.BrandID,
// 			OrderID:               afterSalePtr.OrderID,
// 			OrderSerialNumber:     afterSalePtr.OrderSerialNumber,
// 			AfterSaleID:           afterSalePtr.ID,
// 			AfterSaleSerialNumber: afterSalePtr.SerialNumber,
// 			LedgerRequestPtr:      ledgerRequestPtrAtOrderStatusToFinish,
// 			SubmchProfitAmount:    submchProfitAmount,
// 			PlatformCommission:    ledgerRequestPtrAtOrderStatusToFinish.PlatformCommission,
// 			DistributorCommission: ledgerRequestPtrAtOrderStatusToFinish.DistributorCommission,
// 			BonusAmount:           ledgerRequestPtrAtOrderStatusToFinish.BonusAmount,
// 			ProfitTrigger:         sqlc.EmProfitTriggerAtAfterSaleRefunded,
// 		})
// 		if err != nil {
// 			return err
// 		}
// 		err = s.RollbackToProxy(ctx, q, &ledgerRollback, ledgerRollDetailPtrs)
// 		if err != nil {
// 			slog.Infof("售后单(%s)分账回退(%s)请求失败:%s", afterSaleSerialNumber, ledgerRollback.SerialNumber, err.Error())
// 			return err
// 		}
// 	}

// 	return
// }

// createLedgerRollbackAndDetailsForAfterSaleBackup 订单已完成且部分退场景->根据售后单创建分账回退和明细(分账回退和分账请求不对应)
// func (s *LedgerServiceImp) createLedgerRollbackAndDetailsForAfterSaleBackup(ctx context.Context, q *sqlc.Queries, orderPtr *sqlc.Order, afterSalePtr *sqlc.AfterSale, ledgerRequests []sqlc.LedgerRequest) (err error) {
// 	slog := util.NewContextLogger(ctx, s.slog)
// 	afterSaleSerialNumber := afterSalePtr.SerialNumber

// 	// 对已完成的分账进行分账回退申请,只要对着之前订单完成的申请原样回退即可
// 	// 目前仅订单完成时触发的商户的冻结金额预先分账一条

// 	ledgerRequestPtrAtOrderStatusToFinish, ok := s.FindLedgerRequest(ledgerRequests, func(lr *sqlc.LedgerRequest) bool {
// 		return lr.LedgerStatus == string(sqlc.EmLedgerStatusFinished) && lr.ProfitTrigger == string(sqlc.EmProfitTriggerAtOrderStatusToFinish)
// 	})

// 	if ok {
// 		returnPlatformCommission := ledgerRequestPtrAtOrderStatusToFinish.PlatformCommission
// 		returnDistributorCommission := ledgerRequestPtrAtOrderStatusToFinish.DistributorCommission
// 		returnBonusAmount := ledgerRequestPtrAtOrderStatusToFinish.BonusAmount
// 		var returnSubmchProfitAmount int64
// 		if afterSalePtr.IsWholeRefund {
// 			returnSubmchProfitAmount = ledgerRequestPtrAtOrderStatusToFinish.SubmchProfitAmount
// 		} else {
// 			newUnSplitAmount := orderUtil.CalcUnsplitAmount(orderPtr)
// 			var newSubmchProfitAmount int64

// 			// 按比例折算需要回退的金额
// 			ledgerRequestPtrAtAfterSaleDeadline, ok2 := s.FindLedgerRequest(ledgerRequests, func(lr *sqlc.LedgerRequest) bool {
// 				return lr.LedgerStatus == string(sqlc.EmLedgerStatusCreated) && lr.ProfitTrigger == string(sqlc.EmProfitTriggerAtAfterSaleDeadline)
// 			})
// 			if ok2 {
// 				var newPlatformCommission, newDistributorCommission, newBonusAmount int64

// 				platformCommissionForAfterSale := afterSalePtr.PlatformCommissionAmount
// 				distributorCommissionForAfterSale := afterSalePtr.DistributorCommission
// 				bonusAmountForAfterSale := afterSalePtr.BonusAmount
// 				if platformCommissionForAfterSale > 0 {
// 					newPlatformCommission = ledgerRequestPtrAtAfterSaleDeadline.PlatformCommission - platformCommissionForAfterSale
// 				}
// 				if distributorCommissionForAfterSale > 0 {
// 					newDistributorCommission = ledgerRequestPtrAtAfterSaleDeadline.DistributorCommission - distributorCommissionForAfterSale
// 				}
// 				if bonusAmountForAfterSale > 0 {
// 					newBonusAmount = ledgerRequestPtrAtAfterSaleDeadline.BonusAmount - bonusAmountForAfterSale
// 				}

// 				// 实际商户可分账金额=实际未分配金额+实际的平台佣金+-实际的分销佣金-实际的订单奖励金
// 				slog.Infof("newUnSplitAmount=>%d, newUnSplitAmount=>%d,newPlatformCommission=>%d,newDistributorCommission=>%d,newBonusAmount=>%d", newUnSplitAmount, newUnSplitAmount)
// 				newSubmchProfitAmount = int64(newUnSplitAmount) - newPlatformCommission - newDistributorCommission - newBonusAmount
// 			} else {
// 				newSubmchProfitAmount = int64(newUnSplitAmount)
// 			}

// 			slog.Infof("ledgerRequestPtrAtOrderStatusToFinish=>%d, newSubmchProfitAmount=>%d", ledgerRequestPtrAtOrderStatusToFinish, newSubmchProfitAmount)
// 			returnSubmchProfitAmount = ledgerRequestPtrAtOrderStatusToFinish.SubmchProfitAmount - newSubmchProfitAmount
// 		}

// 		ledgerRollback, err := s.CreateRollback(ctx, q, CreateRollbackParam{
// 			BrandID:               afterSalePtr.BrandID,
// 			OrderID:               afterSalePtr.OrderID,
// 			OrderSerialNumber:     afterSalePtr.OrderSerialNumber,
// 			AfterSaleID:           afterSalePtr.ID,
// 			AfterSaleSerialNumber: afterSalePtr.SerialNumber,
// 			LedgerRequestPtr:      ledgerRequestPtrAtOrderStatusToFinish,
// 			SubmchProfitAmount:    returnSubmchProfitAmount,
// 			PlatformCommission:    returnPlatformCommission,
// 			DistributorCommission: returnDistributorCommission,
// 			BonusAmount:           returnBonusAmount,
// 			ProfitTrigger:         sqlc.EmProfitTriggerAtAfterSaleRefunded,
// 		})
// 		if err != nil {
// 			return err
// 		}
// 		err = s.RollbackToProxy(ctx, q, &ledgerRollback)
// 		if err != nil {
// 			slog.Infof("售后单(%s)分账回退(%s)请求失败:%s", afterSaleSerialNumber, ledgerRollback.SerialNumber, err.Error())
// 			return err
// 		}
// 	}

// 	return
// }

// updateUnSubmitLedgerRequestAndDetailsForAfterSale 修改未提交的订单的分账请求和明细
func (s *LedgerServiceImp) updateUnSubmitLedgerRequestAndDetailsForAfterSale(ctx context.Context, q *sqlc.Queries, orderPtr *sqlc.Order, afterSalePtr *sqlc.AfterSale, ledgerRequests []sqlc.LedgerRequest) (err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	orderSerialNumber := afterSalePtr.OrderSerialNumber
	platformCommissionForAfterSale := afterSalePtr.PlatformCommissionAmount
	distributorCommissionForAfterSale := afterSalePtr.DistributorCommission
	bonusAmountForAfterSale := afterSalePtr.BonusAmount

	ledgerDetailPtrs, err := s.ListLedgerDetailsByOrderSerialNumber(ctx, q, orderSerialNumber)
	if err != nil {
		return
	}
	ledgerRequestPtrAtAfterSaleDeadline, ok := s.FindLedgerRequest(ledgerRequests, func(lr *sqlc.LedgerRequest) bool {
		return lr.LedgerStatus == string(sqlc.EmLedgerStatusCreated) && lr.ProfitTrigger == string(sqlc.EmProfitTriggerAtAfterSaleDeadline)
	})
	var newPlatformCommission, newDistributorCommission, newBonusAmount int64
	if ok {
		// 更新处理平台佣金
		if platformCommissionForAfterSale > 0 {
			newPlatformCommission = ledgerRequestPtrAtAfterSaleDeadline.PlatformCommission - platformCommissionForAfterSale
			ledgerDetailPtrsAtAfterSaleForPlatformCommission, ok2 := s.FindLedgerDetailPtr(ledgerDetailPtrs, func(ld *sqlc.LedgerDetail) bool {
				return ld.LedgerRequestID.Valid &&
					ld.LedgerRequestID.Int64 == ledgerRequestPtrAtAfterSaleDeadline.ID &&
					ld.Category.Valid && ld.Category.String == string(sqlc.EmLedgerCategoryPlatformCommission)
			})
			if ok2 {
				err = q.UpdateLedgerDetailAmount(ctx, sqlc.UpdateLedgerDetailAmountParams{
					ID:     ledgerDetailPtrsAtAfterSaleForPlatformCommission.ID,
					Amount: newPlatformCommission,
				})
				if err != nil {
					return
				}
			}
			slog.Infof("platformCommissionForAfterSale=(%d) ok", platformCommissionForAfterSale)
		} else {
			slog.Infof("platformCommissionForAfterSale=0 skip")
		}

		// 更新分销佣金
		if distributorCommissionForAfterSale > 0 {
			newDistributorCommission = ledgerRequestPtrAtAfterSaleDeadline.DistributorCommission - distributorCommissionForAfterSale
			ledgerDetailPtrsAtAfterSaleForDistributorCommission, ok2 := s.FindLedgerDetailPtr(ledgerDetailPtrs, func(ld *sqlc.LedgerDetail) bool {
				return ld.LedgerRequestID.Valid &&
					ld.LedgerRequestID.Int64 == ledgerRequestPtrAtAfterSaleDeadline.ID &&
					ld.Category.Valid && ld.Category.String == string(sqlc.EmLedgerCategoryOrderDistributorCommission)
			})
			if ok2 {
				err = q.UpdateLedgerDetailAmount(ctx, sqlc.UpdateLedgerDetailAmountParams{
					ID:     ledgerDetailPtrsAtAfterSaleForDistributorCommission.ID,
					Amount: newDistributorCommission,
				})
				if err != nil {
					return
				}
			}
			slog.Infof("distributorCommissionForAfterSale=(%d) ok", distributorCommissionForAfterSale)
		} else {
			slog.Infof("distributorCommissionForAfterSale=0 skip")
		}

		// 更新订单奖励金
		if bonusAmountForAfterSale > 0 {
			newBonusAmount = ledgerRequestPtrAtAfterSaleDeadline.BonusAmount - bonusAmountForAfterSale
			slog.Infof("up order(%s),newBonusAmount=> ledgerRequestPtrAtAfterSaleDeadline.BonusAmount(%d) - bonusAmountForAfterSale(%d)=%d", orderPtr.SerialNumber, ledgerRequestPtrAtAfterSaleDeadline.BonusAmount, bonusAmountForAfterSale, newBonusAmount)
			ledgerDetailPtrsAtAfterSaleForBonusAmount, ok2 := s.FindLedgerDetailPtr(ledgerDetailPtrs, func(ld *sqlc.LedgerDetail) bool {
				return ld.LedgerRequestID.Valid &&
					ld.LedgerRequestID.Int64 == ledgerRequestPtrAtAfterSaleDeadline.ID &&
					ld.Category.Valid && ld.Category.String == string(sqlc.EmLedgerCategoryOrderBonusProfit)
			})
			if ok2 {
				err = q.UpdateLedgerDetailAmount(ctx, sqlc.UpdateLedgerDetailAmountParams{
					ID:     ledgerDetailPtrsAtAfterSaleForBonusAmount.ID,
					Amount: newBonusAmount,
				})
				if err != nil {
					return
				}
			}
			slog.Infof("bonusAmountForAfterSale=(%d) ok", bonusAmountForAfterSale)
		} else {
			slog.Infof("bonusAmountForAfterSale=0 skip")
		}

		err = q.UpdateLedgerRequestAmount(ctx, sqlc.UpdateLedgerRequestAmountParams{
			ID:                    ledgerRequestPtrAtAfterSaleDeadline.ID,
			SubmchProfitAmount:    ledgerRequestPtrAtAfterSaleDeadline.SubmchProfitAmount,
			PlatformCommission:    newPlatformCommission,
			DistributorCommission: newDistributorCommission,
			BonusAmount:           newBonusAmount,
		})
		if err != nil {
			return
		}
		totalAmountAtAfterSaleDeadline := ledgerRequestPtrAtAfterSaleDeadline.SubmchProfitAmount +
			newPlatformCommission + newDistributorCommission + newBonusAmount
		if totalAmountAtAfterSaleDeadline == 0 {
			err = q.CloseZeroAmountUnSubmitLedgerRequest(ctx, ledgerRequestPtrAtAfterSaleDeadline.ID)
			if err != nil {
				return
			}
		}

		slog.Infof("更新分账请求(售后截止时触发) ok")
	}

	if !orderUtil.CheckIsOrderFinish(orderPtr) {
		// 订单未完成
		ledgerRequestPtrAtOrderStatusToFinish, ok3 := s.FindLedgerRequest(ledgerRequests, func(lr *sqlc.LedgerRequest) bool {
			return lr.LedgerStatus == string(sqlc.EmLedgerStatusCreated) && lr.ProfitTrigger == string(sqlc.EmProfitTriggerAtOrderStatusToFinish)
		})
		if ok3 {
			newUnSplitAmount := orderUtil.CalcUnsplitAmount(orderPtr)
			slog.Infof("up order(%s),newUnSplitAmount=> %d", orderPtr.SerialNumber, newUnSplitAmount)
			// 实际商户可分账金额=实际未分配金额+实际的平台佣金+-实际的分销佣金-实际的订单奖励金
			newSubmchProfitAmount := int64(newUnSplitAmount) - newPlatformCommission - newDistributorCommission - newBonusAmount
			slog.Infof("up order(%s),newSubmchProfitAmount=> %d,newBonusAmount=>%d", orderPtr.SerialNumber, newUnSplitAmount, newBonusAmount)

			err = q.UpdateLedgerRequestAmount(ctx, sqlc.UpdateLedgerRequestAmountParams{
				ID:                    ledgerRequestPtrAtOrderStatusToFinish.ID,
				SubmchProfitAmount:    newSubmchProfitAmount,
				PlatformCommission:    ledgerRequestPtrAtOrderStatusToFinish.PlatformCommission,
				DistributorCommission: ledgerRequestPtrAtOrderStatusToFinish.DistributorCommission,
				BonusAmount:           ledgerRequestPtrAtOrderStatusToFinish.BonusAmount,
			})
			if err != nil {
				return
			}

			totalAmountAtOrderStatusToFinish := newSubmchProfitAmount +
				ledgerRequestPtrAtOrderStatusToFinish.PlatformCommission +
				ledgerRequestPtrAtOrderStatusToFinish.DistributorCommission +
				ledgerRequestPtrAtOrderStatusToFinish.BonusAmount
			if totalAmountAtOrderStatusToFinish == 0 {
				err = q.CloseZeroAmountUnSubmitLedgerRequest(ctx, ledgerRequestPtrAtOrderStatusToFinish.ID)
				if err != nil {
					return
				}
			}

			ledgerDetailPtrsAtAfterSaleForSubmchProfit, ok4 := s.FindLedgerDetailPtr(ledgerDetailPtrs, func(ld *sqlc.LedgerDetail) bool {
				return ld.LedgerRequestID.Valid &&
					ld.LedgerRequestID.Int64 == ledgerRequestPtrAtOrderStatusToFinish.ID &&
					ld.Category.Valid && ld.Category.String == string(sqlc.EmLedgerCategorySubmchProfit)
			})
			if ok4 {
				slog.Infof("up order(%s),ledger_detail for subprofit(%d) ok", orderPtr.SerialNumber, newUnSplitAmount)
				err = q.UpdateLedgerDetailAmount(ctx, sqlc.UpdateLedgerDetailAmountParams{
					ID:     ledgerDetailPtrsAtAfterSaleForSubmchProfit.ID,
					Amount: newSubmchProfitAmount,
				})
				if err != nil {
					return
				}
			}

			slog.Infof("订单未完结-更新分账请求(订单完成时触发) ok")
		}
	}

	slog.Infof("UpdateUnSubmitLedgerRequestAndDetailsForAfterSaleBeforeOrderFinished-修改未完成订单的分账请求和明细 ok")
	return
}

// closeUnSubmitLedgerRequestsBySerialNumber 关闭订单的所有未提交的分账请求
func (s *LedgerServiceImp) closeUnSubmitLedgerRequestsBySerialNumber(ctx context.Context, q *sqlc.Queries, orderSerialNumber string) (err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	err = q.CloseUnSubmitLedgerRequestsBySerialNumber(ctx, orderSerialNumber)
	if err != nil {
		return
	}
	slog.Infof("CloseLedgerRequestsByOrderSerialNumber-关闭未完成订单的所有分账请求 ok")
	return
}
